/*
 * Copyright (c) 2018 maoyan.com
 * All rights reserved.
 *
 */
package com.darren.euc.util;

import com.darren.euc.entity.BookTimeRange;
import com.google.common.collect.Lists;
import org.joda.time.DateTime;
import org.joda.time.LocalTime;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 在这里编写类的功能描述
 *
 * @author fangwenhao
 * @created 2018/3/11
 */
public class DateUtil {

    public static Date string2Date(String strDate, String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<BookTimeRange> gene(String startTime, String endTime){
        LocalTime d1 = new LocalTime(startTime);
        LocalTime d2 = new LocalTime(endTime);
        List<BookTimeRange> bookTimeRanges = Lists.newArrayList();
        Integer i = 0;
        while (!d1.toString("HH:mm").equals(d2.toString("HH:mm"))){
            i++;
            BookTimeRange bookTimeRange = new BookTimeRange();
            bookTimeRange.setId(i);
            bookTimeRange.setTime(d1.toString("HH:mm"));
            String endTime1 = new String("23:59");
            String endTime2 = new String("23:30");
            if(d2.toString("HH:mm").equals(endTime1) && d1.toString("HH:mm").equals(endTime2)){
            	d1 = d1.plusMinutes(29);
            }else{
            	d1 = d1.plusMinutes(30);
            }
            bookTimeRange.setStatus(0);
            bookTimeRanges.add(bookTimeRange);
        }

        return bookTimeRanges;

    }

    public static String addHalfHour(String time){
        LocalTime lt = new LocalTime(time);
        return lt.plusMinutes(30).toString("HH:mm");
    }


    /**
     * 获取指定日期当月的第一天
     * @param dateStr
     * @param format
     * @return
     */
    public static String getFirstDayOfGivenMonth(String dateStr,String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            Date date = sdf.parse(dateStr);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.DAY_OF_MONTH,1);
            calendar.add(Calendar.MONTH, 0);
            return sdf.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**

     * 获取指定日期下个月的第一天
     * @param dateStr
     * @param format
     * @return
     */
    public static String getFirstDayOfNextMonth(String dateStr,String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            Date date = sdf.parse(dateStr);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.DAY_OF_MONTH,1);
            calendar.add(Calendar.MONTH, 1);
            return sdf.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 当月天数列表
     * @param dateStr
     * @return
     */
    public static List<String> getDaysOfCurMon(String dateStr){
        String abDayCurMon = dateStr+"-15";
        DateTime begin = DateTime.parse(getFirstDayOfGivenMonth(abDayCurMon,"yyyy-MM-dd"));
        DateTime end = DateTime.parse(getFirstDayOfNextMonth(abDayCurMon,"yyyy-MM-dd"));
        List<String> days = Lists.newArrayList();
        while (begin.isBefore(end)){
            days.add(begin.toString("yyyy-MM-dd"));
            begin = begin.plusDays(1);
        }
        return days;
    }

    /**
     * 当月已过天数列表
     * @param dateStr
     * @return
     */
    public static List<String> getDaysOfCurMonEd(String dateStr){
        String abDayCurMon = dateStr+"-15";
        DateTime begin = DateTime.parse(getFirstDayOfGivenMonth(abDayCurMon,"yyyy-MM-dd"));
        DateTime end = DateTime.parse(getFirstDayOfNextMonth(abDayCurMon,"yyyy-MM-dd"));
        List<String> days = Lists.newArrayList();
        while (begin.isBefore(end) && begin.isBefore(new DateTime())){
            days.add(begin.toString("yyyy-MM-dd"));
            begin = begin.plusDays(1);
        }
        return days;
    }

    /**
     * 将短时间格式时间转换为字符串 yyyy-MM-dd
     *
     * @param dateDate
     * @param format
     * @return
     */
    public static String dateToStr(Date dateDate,String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    /**
     * yyyy-MM-dd的初始
     *
     * @param dateStr
     * @return
     */
    public static Date str2Start(String dateStr) {
       return new DateTime(dateStr).withMillisOfDay(0).toDate();
    }

    /**
     * yyyy-MM-dd的结束
     *
     * @param dateStr
     * @return
     */
    public static Date str2End(String dateStr) {
        return new DateTime(dateStr).plusDays(1).withMillisOfDay(0).toDate();
    }


    public static void main(String[] args) {
        List<String> da = getDaysOfCurMonEd("2018-02-02");
        System.out.println(da.size());
    }

}
